package principal;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.KeyStroke;

import dao.UsuarioDAO;
import entidade.Usuario;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class JDLogin extends JDialog {

	private JPanel contentPane;
	private JPanel fieldPane;
	private JPanel buttonPane;
	private JLabel lbLogin;
	private JLabel lbSenha;
	private JTextField tFLogin;
	private JPasswordField pFSenha;
	private JButton btnOk;
	private JButton btnCancelar;

	public JDLogin(JFrame frame) {
		super(frame);
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				JDLogin.class.getResource("/img/coingold.png")));
		setBounds(100, 100, 348, 200);
		setLocationRelativeTo(this);
		setTitle("Acesso ao Sistema");
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setModal(true);

		contentPane = new JPanel(new BorderLayout());
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		fieldPane = new JPanel(new GridBagLayout());
		contentPane.add(fieldPane, BorderLayout.CENTER);

		lbLogin = new JLabel("Login:");
		fieldPane.add(lbLogin, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.EAST, GridBagConstraints.EAST,
				new Insets(2, 2, 2, 2), 0, 0));

		tFLogin = new JTextField();
		fieldPane.add(tFLogin, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 2), 0, 0));

		lbSenha = new JLabel("Senha:");
		fieldPane.add(lbSenha, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.EAST, GridBagConstraints.EAST,
				new Insets(2, 2, 2, 2), 0, 0));

		pFSenha = new JPasswordField();
		fieldPane.add(pFSenha, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 2), 0, 0));

		buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 5));
		contentPane.add(buttonPane, BorderLayout.SOUTH);

		btnOk = new JButton("Ok");
		btnOk.setMnemonic('O');
		btnOk.setContentAreaFilled(false);
		btnOk.setIcon(new ImageIcon(JDLogin.class.getResource("/img/tick.png")));
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UsuarioDAO udao = new UsuarioDAO();
				Usuario u = new Usuario();
				u.setLogin(tFLogin.getText());
				u.setSenha(String.valueOf(pFSenha.getPassword()));
				u = udao.validaUsuario(u);
				if (u != null) {
					JFPrincipal.setUsuario(u);
					dispose();
				} else {
					JOptionPane.showMessageDialog(JDLogin.this,
							"Usuario ou senha incorretos.");
				}
			}
		});
		btnOk.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
				KeyStroke.getKeyStroke("ENTER"), "actionName");
		btnOk.getActionMap().put("actionName",
				new AbstractAction("actionName") {
					public void actionPerformed(ActionEvent evt) {
						btnOk.doClick();
					}
				});
		buttonPane.add(btnOk);

		btnCancelar = new JButton("Cancelar");
		btnCancelar.setContentAreaFilled(false);
		btnCancelar.setIcon(new ImageIcon(JDLogin.class
				.getResource("/img/cross.png")));
		btnCancelar.setMnemonic('C');
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnCancelar.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
				KeyStroke.getKeyStroke("ESCAPE"), "actionName");
		btnCancelar.getActionMap().put("actionName",
				new AbstractAction("actionName") {
					public void actionPerformed(ActionEvent evt) {
						btnCancelar.doClick();
					}
				});
		buttonPane.add(btnCancelar);
	}
}
